import axios from 'axios'
import { ElMessage } from 'element-plus'
import { getCache } from './cache'
const service = axios.create({
  baseURL:'http://152.136.185.210:5000',
  timeout:10000
})
service.interceptors.request.use(config=>{
  const token = getCache('token')
  if (token) {
    config.headers.Authorization = `Bearer ${token}` // 让每个请求携带自定义token
  }
  return config
})

service.interceptors.response.use(response=>{
   if(response.data.code == '403') {
      ElMessage({
          type: 'error',
          message: response.data.message
      })
      store.commit('login/changeLogout');
      return
    }
    return response.data
    },
    err => {
      ElMessage({
          type: 'error',
          message: err
      })
      return Promise.reject(err)
})

export default service